package com.thtfpc.stroke.screening.service;


import com.thtfpc.stroke.screening.bean.MedSampleDistribute;
import com.thtfpc.stroke.screening.bean.MedUsrInfo;
import com.thtfpc.stroke.screening.bean.comm.CommResponse;
import com.thtfpc.stroke.screening.bean.comm.ServerCode;
import com.thtfpc.stroke.screening.dao.MedSampleDistributeMapper;
import com.thtfpc.stroke.screening.dao.MedUsrInfoMapper;
import com.thtfpc.stroke.screening.model.LogInReq;
import com.thtfpc.stroke.screening.model.MedSampleQuery;
import com.thtfpc.stroke.screening.model.SampleDistributeReq;
import com.thtfpc.stroke.screening.util.CodeUtil;
import com.thtfpc.stroke.screening.util.DateConvertUtil;
import com.thtfpc.stroke.screening.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SampleDistributeServiceImp implements  SampleDistributeService {

    @Autowired
    private MedSampleDistributeMapper medSampleDistributeMapper ;

    @Autowired
    private MedUsrInfoMapper medUsrInfoMapper;

    public static final  String[] groups =new String[]{"实验组","对照组"};



    @Override
    public MedSampleDistribute generateDistributeCode(SampleDistributeReq req) {
        MedSampleDistribute data =new MedSampleDistribute();
        data.setHospitalName(req.getHospitalName());
        data.setPatientNum(req.getPatientNum());
        MedSampleDistribute dbData=  medSampleDistributeMapper.selectOne(data);
        if(dbData!=null){
            dbData.setId(null);
            return dbData;
        }
        data.setGroupName(getRandGroup());
        data.setGroupId(generationGroupID(data.getGroupName()));
        data.setPatientName(req.getPatientName());
        data.setCreateTime(new Date());

        medSampleDistributeMapper.insertSelective(data);

        return data;
    }

    @Override
    public CommResponse login(LogInReq req) {
        MedUsrInfo query =new MedUsrInfo();
        query.setAccountMobile(req.getAccountId());
        query.setAccountPwd(req.getPwd());
        query.setIsUse("Y");
        MedUsrInfo data =  medUsrInfoMapper.selectOne(query);
        if(data!=null){
            data.setId(null);
            data.setAccountPwd("");
            return  new CommResponse(data);
        }else{
            return new CommResponse(ServerCode.LOGIN_INFO_ERROR);
        }

    }

    @Override
    public List<MedSampleDistribute> getListByHospitalName(MedSampleQuery req) {
        MedSampleDistribute data =new MedSampleDistribute();
        data.setHospitalName(req.getHospitalName());
        List<MedSampleDistribute> dbData=  medSampleDistributeMapper.select(data);
        return dbData;
    }

    private   static String getRandGroup(){

            return  groups[RandomUtil.getOneOrZero()];
    }


    public  String generationGroupID(String groupName ) {
        String groupSign="";
        if(SampleDistributeServiceImp.groups[0].equals(groupName)){
            groupSign="20";
        }else{
            groupSign="10";
        }
        StringBuffer code = new StringBuffer();
        int seq = medSampleDistributeMapper.getNextVal("experimental_sample_seq");
        code.append(groupSign).append(seq);
        return code.toString();
    }

}
